#include "path.h"


#define SECTOR_SIZE 512
#define ENODE_CANT ((SECTOR_SIZE-sizeof(unsigned int))/sizeof(eNode))

int isLastNode(char * path){
	int i;
	for(i =0; i < strlen(path) && path[i]!= '/';i++);
	return i==strlen(path);
}

int getFirstNode(char * ret, char * path){
	int i;
	for(i =0; i < strlen(path) && path[i]!= '/';i++);
	
	strncpy(ret, path, i-1);
	return i;
}


int recursivePath(char * path){
	
	if(isAbsolutePath(path)) 
		return 1;
	
	int tam=strlen(path);
	char name[50];
	int i=0;
	do{
		i+=getFirstNode(name, path+i);
		i+=1;
		if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0)	return 1;
		
	}while(i<tam);
	return 0;
}

void getCola(char * ret, char * path){
	
	int i;
	for(i =0; i < strlen(path) && path[i]!= '/';i++);
	strcpy(ret, path+i+1);
	return;
}

int getLastNode(char * ret, char * path){
	
	int i;
	for(i = strlen(path)-1; i > 0 && path[i]!= '/';i--);
	if(path[i] == '/') i++;
	strcpy(ret,path+i);
	
	return;
}

int isAbsolutePath(char * path){
	return path[0]=='/';
}


void withoutLastNode(char * ret, char * path){
	
	int i;
	for(i = strlen(path)-1; i > 0 && path[i]!= '/';i--);
	if(i==0){
		strcpy(ret, "/");
		return;
	}
	strncpy(ret,path, i-1);
	
	return;
	
}

void getParentPath(char * ret, char * path){
	
	int i;
	for(i = strlen(path)-1; i > 0 && path[i]!= '/';i--);
	strncpy(ret, path, i-1);
	ret[i]='\0';
	return;
}

void getFilePath(char * ret, char * path){
	
	if(path[0]=='/'){
		strcpy(ret, path);
		return;
	}
	char * cPath=(char *)getPath();
	strcpy(ret, cPath);
	strcpy(ret+strlen(cPath), "/");
	strcpy(ret+strlen(cPath)+1, path);
	return;
}

void getParentLinkPath(char * path){
	
	if(isLastNode(path))
		return;
	
	char parent[MAX_PATH];
	withoutLastNode(parent, path);
	
	char lastNode[MAX_PATH];
	getLastNode(lastNode, path);
	
	getLinkPath(parent);
	strcpy(path,parent);
	strcpy(path+strlen(parent),"/");
	strcpy(path+strlen(parent)+1, lastNode);
	return;
}

void getLinkPath(char * path){
	
	int flag;
	
		int fd=open(path,O_RD);
	
		if(fd==-1){
			return;
		}
		
		char buff[MAX_PATH+2];
		
		if(( __read(fd,buff,MAX_PATH+3)) != -1){
			if(buff[0]='\x1b' && buff[1]=='L'){		
				buff[MAX_PATH+2] = '\0';
				strcpy(path, buff+2);
			}
		}
	
		close(fd);
	
	
	return;
}


void add_enode(int fd ,char * new_file_name, unsigned int child_sector){
	if(!strlen(new_file_name)){
		return;
	}
	
	lseek(fd,0);
	
	char sector[SECTOR_SIZE];
	int offset=0;

	__read(fd,sector,SECTOR_SIZE);
	DirData* dirData=(DirData*)sector;
	
	while(dirData->eNodeCant>=ENODE_CANT){
		offset+=SECTOR_SIZE-1;
		if(__read(fd,sector,SECTOR_SIZE)<SECTOR_SIZE){
			dirData->eNodeCant=0;
			lseek(fd,offset);
			__write(fd,sector,SECTOR_SIZE);
			lseek(fd,offset);
		}
		dirData=(DirData*)sector;
	}

	strcpy(dirData->list[dirData->eNodeCant].name,new_file_name);
	
	dirData->list[dirData->eNodeCant].sector=child_sector;
	dirData->eNodeCant++;
	
	lseek(fd,offset);
	
	__write(fd,sector,SECTOR_SIZE);

}

void rm_enode(int fd ,char * name){
	lseek(fd,0);
	
	char sector[SECTOR_SIZE];
	int offset=0;
	
	while(__read(fd,sector,SECTOR_SIZE)){
	
		DirData* dirData=(DirData*)sector;
		
		int i;
		for(i=0; i< dirData->eNodeCant; i++){
			if(strcmp(dirData->list[i].name,name)==0){
				dirData->eNodeCant--;
				dirData->list[i].sector=dirData->list[dirData->eNodeCant].sector;
				strcpy(dirData->list[i].name,dirData->list[dirData->eNodeCant].name);
				
				
				
				lseek(fd,offset);
				__write(fd,sector,SECTOR_SIZE);
				return;
			}
		}
		offset+=SECTOR_SIZE-1;
	}
}
